package com.zzvcom.service.admin;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzvcom.persist.model.admin.Config;
import com.zzvcom.persist.model.admin.Meta;
import com.zzvcom.persist.model.admin.Product;
import com.zzvcom.persist.model.admin.ProductDes;
import com.zzvcom.persist.model.admin.ProductFoundation;
import com.zzvcom.persist.model.admin.ProductKPSI;
import com.zzvcom.persist.model.admin.ProductPile;
import com.zzvcom.persist.model.admin.ProductQuality;
import com.zzvcom.persist.model.admin.ProductSize;
import com.zzvcom.template.hibernate.DefaultManager;
import com.zzvcom.utils.ObjectUtil;

@Service
@Transactional
public class ProductManager extends DefaultManager<Product, String>
{
    private ProductSizeManager psm;

    private ProductQualityManager pqm;

    private ProductKPSIManager pkm;

    private ProductFoundationManager pfm;

    private ProductPileManager ppm;

    private ConfigManager cm;

    private ImageManager im;
    
    private ProductDesManager pdm;
    
    private MetaManager mm;

    @Transactional
    public void saveProduct(Product product)
    {
        if (StringUtils.isNotBlank(product.getSize()))
        {
            psm.syncSize(product.getSize());
        }
        if (StringUtils.isNotBlank(product.getQuality()))
        {

            pqm.syncLine(product.getQuality());
        }

        if (StringUtils.isNotBlank(product.getKpsi()))
        {

            pkm.syncKPSI(product.getKpsi());
        }

        if (StringUtils.isNotBlank(product.getFoundation()))
        {

            pfm.syncFoundation(product.getFoundation());
        }
        if (StringUtils.isNotBlank(product.getPile()))
        {

            ppm.syncPile(product.getPile());
        }

        if (StringUtils.isBlank(product.getNo()))
        {
            Config config = cm.getConfig();
            cm.updateByHQL("update Config set no=no+1 ", (Object[]) null);
            product.setNo(config.getNoprefix()
                    + ObjectUtil.fillzero5(config.getNo() + 1));
        }     
        this.save(product);       
        
        
        if(StringUtils.isNotBlank(product.getDes()))
        {            
            for(ProductDes aDes:(List<ProductDes>)this.queryByHQL("from ProductDes where productid=?", product.getId()))
            {
                pdm.delete(aDes);
            }
            
            ProductDes aDes=new ProductDes();
            aDes.setContent(product.getDes());
            aDes.setProductid(product.getId());
            pdm.save(aDes);
        }
        
        String metaValue=null;
        
        for(String key :product.getMeta().keySet())
        {
            metaValue=product.getMeta().get(key);            
            //System.out.println(key+"<------------->"+metaValue);
            if(StringUtils.isNotBlank(metaValue))
            {
                for(Meta aMeta:(List<Meta>)this.queryByHQL("from Meta where productid=? and name=?", product.getId(),key))
                {
                    mm.delete(aMeta);
                }
                Meta aMeta=new Meta();
                aMeta.setContent(metaValue);
                aMeta.setName(key);
                aMeta.setProductid(product.getId());
                mm.save(aMeta);
            }
            
        }
    }

    public void deleteProduct(String id)
    {
        Product p = this.get(id);
        this.delete(p);
        im.deleteImageById(p.getImage());
        for(ProductDes aDes:(List<ProductDes>)this.queryByHQL("from ProductDes where productid=?", p.getId()))
        {
            pdm.delete(aDes);
        }
        for(Meta aMeta:(List<Meta>)this.queryByHQL("from Meta where productid=?", p.getId()))
        {
            mm.delete(aMeta);
        }
    }
    
    public void clearIsolate()
    {
        //ProductSizeManager psm
        for(ProductSize ps:psm.getAll())
        {
            if(Long.valueOf(this.queryUniqueByHQL("select count(*) from Product p where p.size=?", ps.getSize()).toString())==0)
            {
                psm.delete(ps);
            }
        }       
        
        //ProductQualityManager pqm
        for(ProductQuality pq:pqm.getAll())
        {
            if(Long.valueOf(this.queryUniqueByHQL("select count(*) from Product p where p.quality=?", pq.getQuality()).toString())==0)
            {
                pqm.delete(pq);
            }
        }            
        //ProductKPSIManager pkm
        for(ProductKPSI pk:pkm.getAll())
        {
            if(Long.valueOf(this.queryUniqueByHQL("select count(*) from Product p where p.kpsi=?",pk.getKpsi()).toString())==0)
            {
                pkm.delete(pk);
            }
        }          
        //ProductFoundationManager pfm
        for(ProductFoundation pf:pfm.getAll())
        {
            if(Long.valueOf(this.queryUniqueByHQL("select count(*) from Product p where p.foundation=?",pf.getFoundation()).toString())==0)
            {
                pfm.delete(pf);
            }
        } 
        
        //ProductPileManager ppm;
        for(ProductPile pp:ppm.getAll())
        {
            if(Long.valueOf(this.queryUniqueByHQL("select count(*) from Product p where p.foundation=?",pp.getPile()).toString())==0)
            {
                ppm.delete(pp);
            }
        }       
    }

    @Autowired
    public void setPsm(ProductSizeManager psm)
    {
        this.psm = psm;
    }

    @Autowired
    public void setIm(ImageManager im)
    {
        this.im = im;
    }

    @Autowired
    public void setPqm(ProductQualityManager pqm)
    {
        this.pqm = pqm;
    }

    @Autowired
    public void setCm(ConfigManager cm)
    {
        this.cm = cm;
    }

    @Autowired
    public void setPkm(ProductKPSIManager pkm)
    {
        this.pkm = pkm;
    }

    @Autowired
    public void setPfm(ProductFoundationManager pfm)
    {
        this.pfm = pfm;
    }
    @Autowired
    public void setPdm(ProductDesManager pdm)
    {
        this.pdm = pdm;
    }
    @Autowired
    public void setMm(MetaManager mm)
    {
        this.mm = mm;
    }

    @Autowired
    public void setPpm(ProductPileManager ppm)
    {
        this.ppm = ppm;
    }

}
